#include <iostream>
#include <bits/ostream.tcc>
//
// Created by Asus on 2025/5/31.
//
using namespace std;

void demo01() {
    int a = 10;
    int *ptr = &a;
    int &ref = a;
    int &ref2 = *ptr;
    cout << "ref:" << ref << endl;
    cout << "&ref:" << &ref << endl;
    cout << "ref2:" << ref2 << endl;
    cout << ptr << endl;
    cout << &a << endl;
    cout << *ptr << endl;

    const int *ptr2 = &a;
    int * const ptr3 = &a;
    // cout << (*ptr2)++ << endl; 报错
    int b = 10;
    ptr2 = &b;
    // ptr3 = &b; 报错
    cout << ++(*ptr3) << endl;
}

void display(int &num) {

}
void display2(int &);
void display3(int *) {

}


void demo02() {
    int a = 10;
    int *ptr = &a;
    cout << "*ptr:" << *ptr << endl;
    cout << "sizeof(ptr):" << sizeof(ptr) << endl;
    cout << "sizeof(a):" << sizeof(a) << endl;
    cout << "sizeof(char *):" << sizeof(char *) << endl;
    cout << "sizeof(float *):" << sizeof(float *) << endl;
    cout << "sizeof(double *):" << sizeof(double *) << endl;
    cout << "sizeof(long *):" << sizeof(long *) << endl;
    cout << "sizeof(long long):" << sizeof(long long) << endl;
    cout << "sizeof(long long *):" << sizeof(long long *) << endl;
}

void demo03() {
    int * p = NULL;
    cout << "p:" << p << endl;
    cout << "sizeof(p):" << sizeof(p) << endl;
    //访问空指针报错
    //内存编号0 ~255为系统占用内存，不允许用户访问
    // cout << *p << endl;
    //指针变量p指向内存地址编号为0x1100的空间
    int * p2 = (int *)0x1100;

    //访问野指针报错
    // cout << *p2 << endl;

    int* ptr = nullptr;
    // *ptr = 10;  // 崩溃！试图解引用空指针
}

void demo04() {
    int a = 10;
    int b = 10;

    //const修饰的是指针，指针指向可以改，指针指向的值不可以更改
    const int * p1 = &a;
    p1 = &b; //正确
    //*p1 = 100;  报错

    //const修饰的是常量，指针指向不可以改，指针指向的值可以更改
    int * const p2 = &a;
    //p2 = &b; //错误
    *p2 = 100; //正确

    //const既修饰指针又修饰常量
    const int * const p3 = &a;
    //p3 = &b; //错误
    //*p3 = 100; //错误
}

void demo05() {
    int arr[] = { 1,2,3,4,5,6,7,8,9,10 };

    int * p = arr;  //指向数组的指针

    cout << "第一个元素： " << arr[0] << endl;
    cout << "指针访问第一个元素： " << *p << endl;

    for (int i = 0; i < 10; i++)
    {
        //利用指针遍历数组
        cout << *p << endl;
        p++;
    }
}
//值传递
void swap1(int a ,int b)
{
    int temp = a;
    a = b;
    b = temp;
}
//地址传递
void swap2(int * p1, int *p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

void demo06() {
    int a = 10;
    int b = 20;
    swap1(a, b); // 值传递不会改变实参

    swap2(&a, &b); //地址传递会改变实参

    cout << "a = " << a << endl;

    cout << "b = " << b << endl;
}

void bubbleSort(int *arr, int len) {
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = 0; j < len - 1 - i; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

//打印数组函数
void printArray(int arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << endl;
    }
}

void demo07() {
    int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
    int len = sizeof(arr) / sizeof(int);
    bubbleSort(arr, len);
    printArray(arr, len);

    int arr2[2][2] = {{1,8}, {2,9}};
    int *p = *arr2;
    int (*ptr)[2] = arr2;
    int (*p2) = arr2[0]; // int arr[] = int *arr,int arr[][] =  int (*arr)
    int (*p3) = arr2[1]; // int arr[] = int *arr,int arr[][] =  int (*arr)
    cout << "p:" << p << endl;
    cout << "*p:" << *p << endl;
    cout << "p2:" << p2 << endl;
    cout << "*p2:" << *p2 << endl;
    cout << "p3:" << p3 << endl;
    cout << "*p3:" << *p3 << endl;
    cout << "ptr[0][0]:" << ptr[0][0] << endl;

    cout << "**arr2:" << **arr2 << endl; // 第一行第一列
    cout << "*arr2:" << *arr2 << endl; // 第一行首地址
    cout << "*(arr2 + 1):" << *(arr2 + 1) << endl; // 第二行首地址
    cout << "*(*(arr2 + 1)):" << *(*(arr2 + 1)) << endl; // 第二行第一列
    cout << "*(*(arr2 + 1)+1):" << *(*(arr2 + 1)+1) << endl; // 第二行第二列

}


int main(char *argv[]) {
    // demo01();
    // demo02();
    // demo03();
    // demo05();
    // demo06();
    demo07();
    return 0;
}
